﻿package  Cain.googleMap.marker
{
	import com.google.maps.LatLng;
	import com.google.maps.Map;
	import com.google.maps.overlays.Marker;
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.overlays.Polyline;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	/**
	 * google地圖移動物件
	 * @author Cain
	 */
	public class MoveMarker extends Marker
	{
		private var v:Number = 30;
		private var latlng:LatLng;
		private var other:MarkerOptions;
		private var map:Map;
		private var golatlng:LatLng;
		private var movla:Number;
		private var movta:Number;
		private var now:LatLng;
		private var timer:Timer;
		/**
		 * 移動物件
		 * @param	_latLng 座標
		 * @param	_other 形態
		 */
		public function MoveMarker(_latLng:LatLng,_other:MarkerOptions = null) 
		{
			latlng = _latLng;
			other = _other;
			
			super(latlng,other);
			
			init();
		}
		/**
		 * 新增到地圖上
		 * @param	_map google地圖
		 */
		public function addthis(_map:Map):void 
		{
			map = _map;
			map.addOverlay(this);
		}
		/**
		 * 依照路徑前進
		 * 移動結束發出"pathend"事件
		 * 每移動一個關鍵座標發出"bend""事件
		 * @param	_path 路徑線條
		 */
		public function gotoDirections(_path:Object):void 
		{
			var n:int = 0;
			var _arr:Array = new Array();
			for (var i:int = 0; i < _path.getVertexCount(); i++) 
			{
				_arr.push(_path.getVertex(i));
			}
			addEventListener("end", next);
			goNextPoint(n);
			function next(e:Event):void 
			{
				
				n++;
				goNextPoint(n);
			}
			function goNextPoint(_n:int):void 
			{
				if (n<_path.getVertexCount()) 
				{
					//dispatchEvent(new Event("_n"));
					dispatchEvent(new Event("bend"));
					goto(_arr[_n]);
				}else 
				{
					removeEventListener("end", next);
					dispatchEvent(new Event("pathend"));
				}
				
			}
		}
		/**
		 * 移動
		 * 移動結束發出"end"事件
		 * @param	_latLng 座標
		 */
		public function goto(_latLng:LatLng):void 
		{
			golatlng = _latLng;
			var n:Number = distance(_latLng) / v;
			if (n>1) //小於1m內部不記
			{
				movla = (golatlng.lat() - now.lat()) / n;
				movta = (golatlng.lng() - now.lng()) / n;
				timer = new Timer(30, n);
				timer.addEventListener(TimerEvent.TIMER, onTIMER);
				timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTIMER_COMPLETE);
				timer.start();
			}else 
			{
				dispatchEvent(new Event("end"));
			}
			
		}
		/**
		 * 橫移動速率
		 */
		public function get vx():Number 
		{
			return movta;
		}
		/**
		 * 移動速率
		 */
		public function get vy():Number 
		{
			return movla;
		}
		private function onTIMER(e:TimerEvent):void 
		{
			now = new LatLng(now.lat() + movla, now.lng() + movta);
			setLatLng(now);
		}
		/**
		 * 停止
		 * @param	_latLng 座標
		 */
		public function stop():void
		{
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER, onTIMER);
		}
		/**
		 * 繼續移動
		 * 
		 */
		public function continuePlay():void 
		{
			timer.addEventListener(TimerEvent.TIMER, onTIMER);
			timer.start();
		}
		/**
		 * 距離(m)
		 * @param	_latLng 座標
		 */
		public function distance(_latLng:LatLng):Number
		{
			var line:Polyline = new Polyline(new Array(now, _latLng), null );
			return line.getLength();
		}
		private function onTIMER_COMPLETE(e:TimerEvent):void 
		{
			dispatchEvent(new Event("end"));
		}
		
		private function init():void
		{
			now = latlng;
		}
		
	}

}